package com.motus.pm.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.motus.pm.dao.DeptTaskDao;
import com.motus.pm.dao.EmpInfoDao;
import com.motus.pm.dao.PhaseInfoDao;
import com.motus.pm.dao.ProjectInfoDao;
import com.motus.pm.dao.ProjectPhaseDao;
import com.motus.pm.dao.ProjectTaskDao;
import com.motus.pm.service.EmpService;
import com.motus.pm.vo.MotusEmpInfo;
import com.motus.pm.vo.PmDeptTask;
import com.motus.pm.vo.PmPhaseInfo;
import com.motus.pm.vo.PmProjectInfo;
import com.motus.pm.vo.PmProjectPhase;
import com.motus.pm.vo.PmProjectTask;
@Service
@Transactional
public class EmpServiceImpl implements EmpService {

	@Autowired
	@Qualifier("projectTaskDaoImpl")
	private ProjectTaskDao projectTaskDao;
	@Autowired
	@Qualifier("projectInfoDaoImpl")
	private ProjectInfoDao projectInfoDao;
	@Autowired
	@Qualifier("phaseInfoDaoImpl")
	private PhaseInfoDao phaseInfoDao;
	@Autowired
	@Qualifier("projectPhaseDaoImpl")
	private ProjectPhaseDao projectPhaseDao;
	@Autowired
	@Qualifier("deptTaskDaoImpl")
	private DeptTaskDao deptTaskDao;
	@Autowired
	@Qualifier("empInfoDaoImpl")
	private EmpInfoDao empInfoDao;
	
	@Override
	public void changeTaskState(PmProjectTask task) {
		// TODO Auto-generated method stub
		projectTaskDao.update(task);
	}
 
	public EmpServiceImpl(){
		
	}
	
	@Override
	public List<PmProjectTask> tasksAll() {
		// TODO Auto-generated method stub
		return projectTaskDao.getAll();
	}
	@Override
	public List<PmProjectTask> tasksByState(int state ,int userId) {
		// TODO Auto-generated method stub
		return projectTaskDao.tasksByState(state,userId);
	}

	@Override
	public PmProjectTask taskById(int taskId) {
		// TODO Auto-generated method stub
//		if(taskId == 0){  //参数为零，返回最近需要完成的项目
//			return projectTaskDao.tasksByState(state, userId)
//		}
		return projectTaskDao.taskById(taskId);
	}
	/**
	 * 通过当前任务任务的任务编号，查询前序任务的相关信息
	 */
	@Override
	public PmProjectPhase aheadTask(int taskId) {
		// TODO Auto-generated method stub
		//获取当前任务信息，获取任务的项目信息，阶段信息
		PmProjectTask  pmTask = projectTaskDao.getById(taskId);  
		PmProjectInfo  pmProject = pmTask.getPmProjectInfo();
		PmPhaseInfo pmPhase = pmTask.getPmPhaseInfo(); 
		
		//获取项目的全部阶段信息,获取该阶段下的全部阶段
		List<PmProjectPhase>  aheadPhaseList = projectPhaseDao.getProjectPhasesByPid(pmProject.getPid());
		
		//将全部阶段存在阶段数组中
		List<PmPhaseInfo>  phaseInfoList = new ArrayList<PmPhaseInfo>();
		for(int i = 0; i < aheadPhaseList.size(); i++){
			phaseInfoList.add(aheadPhaseList.get(i).getId().getPmPhaseInfo());
		}
		
		//分割阶段字符串，进行阶段判断
		char[] phaseChar = pmTask.getPmPhaseInfo().getPhaseId().toCharArray();
		
		//如果当前阶段是D阶段，直接返回
		if("D".equals(phaseChar[1])){ 
			return null;
		}else{         //不在D阶段
			//阶段在数组中的位置
			int k = 0;
			for(int j = 0; j < phaseInfoList.size(); j++){
				//查询阶段
				if(pmTask.getPmPhaseInfo().getPhaseId().equals(phaseInfoList.get(j).getPhaseId())){  
					k = j; //取阶段的位置
				}
			}
			//无前任阶段
			if(k== 0){
				return null;
			}
			//不在第一阶段，找出前序任务，判断是都在同一阶段
			char[] p = phaseInfoList.get(k-1).getPhaseId().toCharArray();
			if(p[1] == phaseChar[1]){
				return aheadPhaseList.get(k-1);
			}
		}
		return null;
	}

	@Override
	public List<PmDeptTask> getDeptTaskByMeetingTypeAndDeptId(int type,int deptId) {
		// TODO Auto-generated method stub
		return deptTaskDao.getDeptTasksByDeptIdAndMeetingType(type, deptId);
	}

	@Override
	public MotusEmpInfo getEmpInfoByName(String name) {
		// TODO Auto-generated method stub
		return empInfoDao.getEmpByName(name);
	}

	@Override
	public int getDeptIdByUserId(int userId) {
		return empInfoDao.getDeptIdByUserId(userId);
	}

}








